package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.SocketMessage;
import com.tgu.home_action.pojo.state.RabbitMq;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

@Controller
public class WebSocketController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @MessageMapping("/volunteer/place")
    public void getPlace(@Payload SocketMessage message, Principal principal) {
        System.out.println("接收到socket");
        String[] user = principal.getName().split("_");
        if (user[0].equals("volunteer")) {
            message.setUserId(user[1]);
            rabbitTemplate.convertAndSend(RabbitMq.VOLUNTEER_PLACE_EXCHANGE.getValue(), RabbitMq.VOLUNTEER_PLACE_SAVE.getValue(), message);
        }
    }
}
